// pages/volunteer/product-reviews.js
Page({
  data: {
    // 商品信息
    productId: null,
    product: {
      id: 1,
      name: '精美陶瓷马克杯',
      image: '/images/杯子.png',
      points: 50
    },
    
    // 评价统计
    totalReviews: 156,
    averageRating: 4.8,
    ratingDistribution: [
      { rating: 5, count: 132, percentage: 85 },
      { rating: 4, count: 16, percentage: 10 },
      { rating: 3, count: 5, percentage: 3 },
      { rating: 2, count: 2, percentage: 1 },
      { rating: 1, count: 1, percentage: 1 }
    ],
    
    // 筛选状态
    currentFilter: 'all',
    filterOptions: [
      { key: 'all', name: '全部', count: 156 },
      { key: 'positive', name: '好评', count: 148 },
      { key: 'neutral', name: '中评', count: 5 },
      { key: 'negative', name: '差评', count: 3 },
      { key: 'withImage', name: '有图', count: 89 }
    ],
    
    // 排序状态
    currentSort: 'time',
    sortOptions: [
      { key: 'time', name: '最新' },
      { key: 'helpful', name: '有用' },
      { key: 'rating', name: '评分' }
    ],
    
    // 评价列表
    reviews: [
      {
        id: 1,
        reviewerName: '张**',
        avatar: '/images/avatar1.jpg',
        stars: '★★★★★',
        rating: 5,
        reviewDate: '2023-12-01',
        content: '杯子质量很好，手感细腻，保温效果也不错。包装很精美，很适合送礼。感谢爱道提供的优质商品！',
        images: ['/images/review1.jpg', '/images/review2.jpg'],
        likes: 12,
        isLiked: false,
        helpful: true,
        verified: true,
        optionName: '标准兑换'
      },
      {
        id: 2,
        reviewerName: '李**',
        avatar: '/images/avatar2.jpg',
        stars: '★★★★★',
        rating: 5,
        reviewDate: '2023-11-28',
        content: '兑换的商品很快就收到了，物流很给力。杯子做工精细，容量适中，日常使用很方便。',
        images: [],
        likes: 8,
        isLiked: true,
        helpful: false,
        verified: true,
        optionName: '豪华套装'
      },
      {
        id: 3,
        reviewerName: '王**',
        avatar: '/images/avatar3.jpg',
        stars: '★★★★☆',
        rating: 4,
        reviewDate: '2023-11-25',
        content: '整体还不错，就是颜色比图片稍微淡一点，不过不影响使用。客服态度很好，解答很及时。',
        images: ['/images/review3.jpg'],
        likes: 5,
        isLiked: false,
        helpful: false,
        verified: true,
        optionName: '标准兑换'
      },
      {
        id: 4,
        reviewerName: '赵**',
        avatar: '/images/avatar4.jpg',
        stars: '★★★★★',
        rating: 5,
        reviewDate: '2023-11-20',
        content: '第二次兑换这个商品了，质量一如既往的好。这次给朋友也兑换了一个，他们都很喜欢。',
        images: ['/images/review4.jpg', '/images/review5.jpg'],
        likes: 15,
        isLiked: false,
        helpful: true,
        verified: true,
        optionName: '礼品套装'
      },
      {
        id: 5,
        reviewerName: '陈**',
        avatar: '/images/avatar5.jpg',
        stars: '★★★★☆',
        rating: 4,
        reviewDate: '2023-11-15',
        content: '杯子很实用，每天上班都用它泡茶。就是希望以后能出更多颜色选择。',
        images: [],
        likes: 6,
        isLiked: false,
        helpful: false,
        verified: false,
        optionName: '标准兑换'
      }
    ],
    
    // 筛选后的评价
    filteredReviews: [],
    
    // 页面状态
    loading: false,
    hasMore: false,
    currentPage: 1,
    pageSize: 10,
    
    // 弹窗状态
    showFilterModal: false,
    showSortModal: false
  },

  onLoad(options) {
    const productId = options.id;
    this.setData({ productId });
    this.loadProductInfo(productId);
    this.loadReviews();
    this.filterReviews();
  },

  // 加载商品信息
  loadProductInfo(productId) {
    // 这里应该调用API获取商品信息
    console.log('加载商品信息:', productId);
  },

  // 加载评价数据
  loadReviews() {
    this.setData({ loading: true });
    
    // 这里应该调用API获取评价数据
    console.log('加载商品评价:', this.data.productId);
    
    setTimeout(() => {
      this.setData({ loading: false });
    }, 500);
  },

  // 筛选评价
  filterReviews(e) {
    const filterType = e ? e.currentTarget.dataset.type : this.data.currentFilter;
    let filteredReviews = this.data.reviews;
    
    switch (filterType) {
      case 'positive':
        filteredReviews = this.data.reviews.filter(review => review.rating >= 4);
        break;
      case 'neutral':
        filteredReviews = this.data.reviews.filter(review => review.rating === 3);
        break;
      case 'negative':
        filteredReviews = this.data.reviews.filter(review => review.rating <= 2);
        break;
      case 'withImage':
        filteredReviews = this.data.reviews.filter(review => 
          review.images && review.images.length > 0
        );
        break;
      default:
        // 'all' - 显示所有评价
        break;
    }
    
    // 应用排序
    filteredReviews = this.sortReviews(filteredReviews, this.data.currentSort);
    
    this.setData({
      currentFilter: filterType,
      filteredReviews: filteredReviews
    });
  },

  // 排序评价
  sortReviews(reviews, sortType) {
    const sortedReviews = [...reviews];
    
    switch (sortType) {
      case 'time':
        sortedReviews.sort((a, b) => new Date(b.reviewDate) - new Date(a.reviewDate));
        break;
      case 'helpful':
        sortedReviews.sort((a, b) => b.likes - a.likes);
        break;
      case 'rating':
        sortedReviews.sort((a, b) => b.rating - a.rating);
        break;
    }
    
    return sortedReviews;
  },

  // 切换排序
  changeSort(e) {
    const sortType = e.currentTarget.dataset.type;
    const sortedReviews = this.sortReviews(this.data.filteredReviews, sortType);
    
    this.setData({
      currentSort: sortType,
      filteredReviews: sortedReviews,
      showSortModal: false
    });
  },

  // 点赞评价
  likeReview(e) {
    const reviewId = e.currentTarget.dataset.id;
    const reviews = this.data.reviews.map(review => {
      if (review.id === reviewId) {
        return {
          ...review,
          isLiked: !review.isLiked,
          likes: review.isLiked ? review.likes - 1 : review.likes + 1
        };
      }
      return review;
    });
    
    this.setData({ reviews });
    this.filterReviews(); // 重新筛选以更新显示
    
    wx.showToast({
      title: reviews.find(r => r.id === reviewId).isLiked ? '已点赞' : '已取消点赞',
      icon: 'none'
    });
  },

  // 回复评价
  replyReview(e) {
    const reviewId = e.currentTarget.dataset.id;
    const review = this.data.reviews.find(r => r.id === reviewId);
    
    wx.showModal({
      title: '回复评价',
      content: `回复 ${review.reviewerName} 的评价`,
      editable: true,
      placeholderText: '请输入回复内容...',
      confirmText: '发送',
      cancelText: '取消',
      success: (res) => {
        if (res.confirm && res.content) {
          // 这里应该调用API发送回复
          wx.showToast({
            title: '回复已发送',
            icon: 'success'
          });
        }
      }
    });
  },

  // 预览图片
  previewImage(e) {
    const current = e.currentTarget.dataset.current;
    const urls = e.currentTarget.dataset.urls;
    
    wx.previewImage({
      current,
      urls
    });
  },

  // 显示筛选弹窗
  showFilter() {
    this.setData({
      showFilterModal: true
    });
  },

  // 关闭筛选弹窗
  closeFilterModal() {
    this.setData({
      showFilterModal: false
    });
  },

  // 显示排序弹窗
  showSort() {
    this.setData({
      showSortModal: true
    });
  },

  // 关闭排序弹窗
  closeSortModal() {
    this.setData({
      showSortModal: false
    });
  },

  // 加载更多评价
  loadMore() {
    if (this.data.loading || !this.data.hasMore) {
      return;
    }
    
    this.setData({ loading: true });
    
    // 模拟加载更多数据
    setTimeout(() => {
      // 这里应该调用实际的API
      this.setData({
        loading: false,
        hasMore: false // 暂时设置为false，实际应该根据API返回结果判断
      });
    }, 1000);
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.loadReviews();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  },

  // 分享页面
  onShareAppMessage() {
    return {
      title: `看看大家对${this.data.product.name}的评价`,
      path: `/pages/volunteer/product-reviews?id=${this.data.productId}`,
      imageUrl: this.data.product.image
    };
  }
});
